<?php
class Blog_Block_Search extends Epic_View_Block 
{		
	public function createPagination()
	{
		$search = htmlspecialchars($_GET['q'], ENT_COMPAT, "UTF-8");
		$totalArticles = Epic::getAdapter()->query('SELECT COUNT(*) AS cnt
													FROM `blog_articles`
													WHERE `title` LIKE "%' . Epic::getAdapter()->quote($search) . '%"
													AND `active` = 1')
										   ->fetchOne();
		parent::createPagination($totalArticles['cnt']);
		//$this->getPagination()->setItemsPerPage(2)->createPagination();
	}
	
	/**
	 * Get articles by search title
	 *
	 * @return array rows retrieve form database
	 */
	public function getArticlesBySearchTitle()
	{	
		// prevent XSS
		$search = htmlspecialchars($_GET['q'], ENT_COMPAT, "UTF-8");
		$currentPage = isset($_GET['page']) ? (int) $_GET['page'] : 1;
		$itemsPerPage = 10;
		$offset = ($currentPage - 1) * $itemsPerPage;
		
		$this->createPagination();
		
        return Epic::getAdapter()->query('SELECT * 
			        						FROM `blog_articles` 
			        					   	  WHERE `title` LIKE "%' . Epic::getAdapter()->quote($search) . '%"
			        						    AND `active` = 1
			        							  ORDER BY `article_id` DESC
			        					   	        LIMIT ' . $offset . ',' . $itemsPerPage)
  					  			 ->fetchAll();
	}
}